package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleSetingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = RoleSetingService.class)
@Transactional
public class RoleSetingServiceImpl implements RoleSetingService {

    @Autowired
    private RoleSetingDao roleSetingDao;
    @Autowired
    private PermissionDao permissionDao;
    //分页查询
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleSetingDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Role> result = page.getResult();
        return new PageResult(total, result);
    }

    //新增数据回显
    public Map findAll() {
        List<Permission> permissions = roleSetingDao.findAllPermissions();
        List<Menu> menus = roleSetingDao.findAllmenus();
        Map<String, List> map = new HashMap<>();
        map.put("permissions", permissions);
        map.put("menus", menus);

        return map;
    }


    //新增角色，同时关联检查组
    public void add(Role role, Integer[] permissions, Integer[] menus) {
        roleSetingDao.add(role);
        Integer roleId = role.getId();//获取角色id
        this.setRoleAndpermissions(roleId, permissions);
        this.setRoleAndmenus(roleId, menus);


    }

    //编辑角色
    public void edit(Role role, Integer[] permissions, Integer[] menus) {
        Integer id = role.getId();
        if (id!=null) {
            roleSetingDao.deletePermissions(id);
            roleSetingDao.deletMeuns(id);
        }
        this.setRoleAndpermissions(id, permissions);
        this.setRoleAndmenus(id, menus);
    }

    //删除角色
    public void delete(int id) {
        //删除关联表的角色信息
        roleSetingDao.deletePermissions(id);
        roleSetingDao.deletMeuns(id);
        //删除角色表中信息
        roleSetingDao.delete(id);

    }

    //编辑的数据回显
    public Map editecho(int id) {
        Role role = roleSetingDao.findRoleById(id);
        List<Menu> meuns = roleSetingDao.findByRoleId(id);
        List<Permission> permissions = permissionDao.findByRoleId2(id);
        List meunsArray = new ArrayList<>();
        List permissionsArray = new ArrayList<>();
        for (Menu m:
            meuns ) {
            meunsArray.add(m.getId());
        }
        for (Permission permission:
                permissions) {
            permissionsArray.add(permission.getId());
        }
        Map map = new HashMap<String,Object>();
        map.put("menus",meunsArray);
        map.put("permissions",permissionsArray);
        map.put("role",role);
        return map;
    }

    public void setRoleAndpermissions(Integer roleId, Integer[] permissions) {
        if (permissions != null && permissions.length > 0) {
            for (Integer permission : permissions) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permission);
                roleSetingDao.setRoleAndpermissions(map);
            }
        }
    }

    public void setRoleAndmenus(Integer roleId, Integer[] menus) {
        if (menus != null && menus.length > 0) {
            for (Integer menu : menus) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menu);
                roleSetingDao.setRoleAndmenus(map);
            }
        }
    }
}
